package de.fhhof.robot.control;

import java.io.Serializable;

import de.fhhof.robot.control.common.Utility;
import de.fhhof.robot.control.net.IConnection;




public class Message implements IMessage, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String msgId;
	
	private String senderId;
	
	private String receiverId;
	
	private String payLoad;
	
	private String command;
	
	private String msgType;
	
	private String messageNo;
	
	private String header;
	
	private String connPrms;
	
	public Message() {
		super();
	}

	public Message(String cmd,String payLoad) {
		this.command = cmd;
		this.payLoad = payLoad;
		this.constructMessage();
	}

	public String getMsgType() {
		return msgType;
	}

	@Override
	public String getMsgId() {
		return this.msgId;
	}

	@Override
	public String getSender() {
		return this.senderId;
	}

	@Override
	public String getReceiver() {
		return this.receiverId;
	}

	@Override
	public String getCommand() {
		return this.command;
	}

	@Override
	public String getPayload() {
		return this.payLoad;
	}

	@Override
	public void setCommand(String command) {
		this.command = command;
		
	}

	@Override
	public void setPayload(String payload) {
		this.payLoad = payload;
		
	}

	@Override
	public void ack(String command, String userMessage) {
		IMessage msg = new Message(command, userMessage);
		ConnectionFactory.getOpenedConnection(connPrms).send(msg);
		//this.conn.send(msg);
	}

	private int getPayLoadSize(){
		if(this.payLoad == null || this.payLoad.equals("")){
			return 0;
		}
		return this.payLoad.length();
	}
	private void constructMessage(){
		this.msgType = this.command.substring(0, 1);
		this.messageNo = this.command.substring(1);
		this.msgId = MessageIdGenerator.getNextMessageId();
		
	}
	private String toASCIIString(){
		IConnection conn = ConnectionFactory.getOpenedConnection(connPrms);
		this.senderId = conn.getSenderId();
		this.receiverId = conn.getReceiverId();
		StringBuffer buffer = new StringBuffer();
		buffer.append(this.senderId);
		buffer.append(this.receiverId);
		buffer.append(this.msgType);
		buffer.append(pad(this.messageNo,3));
		buffer.append(this.msgId);
		buffer.append(pad(getPayLoadSize()+"", 4));
	
		return buffer.toString();
	}
	
	@Override
	public void parse() {
		this.senderId = this.header.substring(0, 2);
		this.receiverId = this.header.substring(2,4);
		this.msgType = this.header.substring(4,5);
		this.messageNo = this.header.substring(5,8);
		this.msgId = this.header.substring(8, 21);
		this.command = this.header.substring(4, 8);
				
	}	
	private String pad(String data,int size){
		return Utility.leftPad(data, "0", size);
	}
	
	public byte[] getRawMessage(){
		String asciiNumber = this.toASCIIString();
		return asciiNumber.getBytes();
	}

	@Override
	public void setHeader(String header) {
		this.header = header;
		
	}



	@Override
	public String toString() {
		return "Message [msgId=" + msgId + ", senderId=" + senderId
				+ ", receiverId=" + receiverId + ", payLoad=" + payLoad
				+ ", command=" + command + ", msgType=" + msgType
				+ ", messageNo=" + messageNo + ", header=" + header + "]";
	}

	@Override
	public void setConnection(String conn) {
		this.connPrms = conn;
		
	}
	

}
